package com.nativetalk.service.comment.insert;

import com.nativetalk.base.RetInfo;
import com.nativetalk.bean.comment.TdNtCommentInsert;
import com.nativetalk.bean.comment.TdNtCommentPage;
import com.nativetalk.bean.comment.TdNtTeacherComment;
import com.nativetalk.bean.course.TsNtCourseInfoDetail;
import com.nativetalk.bean.member.TdNtMemberDetail;
import com.nativetalk.bean.reserve.TdNtReserveInsert;
import com.nativetalk.bean.study.TdNtStudyTeachComment;
import com.nativetalk.bean.teacher.TdNtTeacherResult;
import com.nativetalk.common.Constants;
import com.nativetalk.common.ValidationMessages;
import com.nativetalk.common.getui.AndroidCommentTeacher;
import com.nativetalk.common.getui.AndroidReserveTeacher;
import com.nativetalk.common.getui.IosCommentTeacher;
import com.nativetalk.common.util.CommonUtil;
import com.nativetalk.dao.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by lizhun on 15/12/29.
 * Modified by wangzheng on 17/03/09
 */
@Service
public class CommentInsertServiceImpl implements  CommentInsertService{
    private static Logger logger = Logger.getLogger(CommentInsertServiceImpl.class);
    @Resource
    private TdNtCommentDao tdNtCommentDao;
    @Resource
    private TdNtStudyTeachDao tdNtStudyTeachDao;
    @Resource
    private TdNtCourseInstanceRecordingDao tdNtCourseInstanceRecordingDao;
    @Resource
    private TdNtTeacherDao tdNtTeacherDao;
    @Resource
    private TdNtMemberDao tdNtMemberDao;
    @Resource
    private TsNtCourseInfoDao tsNtCourseInfoDao;


/*
 @Override
    public synchronized RetInfo insertTdNtComment(TdNtCommentInsert tdNtCommentInsert) {
        String logInfo = this.getClass().getName() + ":insertTdNtComment:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
            TdNtStudyTeachComment tdNtStudyTeachComment = tdNtStudyTeachDao.findTdNtStudyTeachComment(tdNtCommentInsert.getStt_id());
            // 1 评论不能为空；2 评价学员是上课学员；3 评论状态为3（待评论）【Study_state：0找寻老师中1学习中2待支付3待评价4已完成5已完成】
            if (tdNtStudyTeachComment != null && tdNtCommentInsert.getMemb_id()!=tdNtStudyTeachComment.getMemb_id() && tdNtStudyTeachComment.getStudy_state().equals("3")) {
                //TdNtMembRecordingList tdNtMembRecordingList = tdNtCourseInstanceRecordingDao.findTdNtMembRecordingBySttId(tdNtCommentInsert.getStt_id());
                tdNtCommentInsert.setCom_month(CommonUtil.getMonth(new Date()));
                tdNtCommentInsert.setCou_id(tdNtStudyTeachComment.getCou_id());
                tdNtCommentInsert.setMemb_id(tdNtStudyTeachComment.getMemb_id());
                tdNtCommentInsert.setTea_id(tdNtStudyTeachComment.getTea_id());
                //tdNtCommentInsert.setRecording_url(tdNtMembRecordingList.getRecording_url());
                tdNtCommentDao.insertTdNtComment(tdNtCommentInsert);
                */
/**
                 * 更新学习评分
                 *//*

                Map<String , Object> map = new HashMap<>();
                map.put("study_comment",tdNtCommentInsert.getCom_grade());
                map.put("stt_id",tdNtCommentInsert.getStt_id());
                map.put("study_state",4);
                tdNtStudyTeachDao.updateTdNtStudyTeachComment(map);

                */
/**
                 * 给老师端发送推送
                 *//*

                TdNtTeacherResult tdNtTeacherResult = tdNtTeacherDao.findTeacherByTeaId(tdNtStudyTeachComment.getTea_id());
                if (tdNtTeacherResult.getEqu_client_id() != null && !(tdNtTeacherResult.getEqu_client_id().equals("1212313fasdf"))) {

                    TdNtMemberDetail tdNtMemberDetail = tdNtMemberDao.findTdNtMemberDetail(tdNtStudyTeachComment.getMemb_id());
                    TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDao.findCourseByCouId(tdNtStudyTeachComment.getCou_id());

                    if (tdNtTeacherResult.getEqu_type().equals(Constants.equ_type_android)) {
                        AndroidCommentTeacher.apnpush("NativeTalk","One student sent you a teaching comment, check now! ",
                                tdNtTeacherResult.getEqu_client_id(),tdNtCommentInsert.getStt_id(),tdNtMemberDetail.getMemb_name(),tdNtMemberDetail.getMemb_head_portrait()
                                ,tdNtTeacherResult.getToken(),tdNtTeacherResult.getTea_head_portrait(),tdNtTeacherResult.getTea_name(),tsNtCourseInfoDetail.getCou_english_name());
                    } else {
                        logger.info(tdNtTeacherResult.getEqu_client_id());
                        IosCommentTeacher.apnpush("NativeTalk","One student sent you a teaching comment, check now! ",
                                tdNtTeacherResult.getEqu_client_id(),tdNtCommentInsert.getStt_id(),tdNtMemberDetail.getMemb_name(),tdNtMemberDetail.getMemb_head_portrait()
                                ,tdNtTeacherResult.getToken(),tdNtTeacherResult.getTea_head_portrait(),tdNtTeacherResult.getTea_name(),tsNtCourseInfoDetail.getCou_english_name());
                    }

                }

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("添加评论成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("TIPAUTHORERROR"));
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
*/

    @Autowired
    private TdNtReserveDao tdNtReserveDao;

    @Transactional
    public synchronized RetInfo insertComment(TdNtCommentPage tdNtCommentPage) {
        String logInfo = this.getClass().getName() + ":insertTdNtComment:";
        logger.info("======" + logInfo + "begin======");
        RetInfo retInfo = new RetInfo();
        try {
           // TdNtStudyTeachComment tdNtStudyTeachComment = tdNtStudyTeachDao.findTdNtStudyTeachComment(tdNtCommentInsert.getStt_id());
            TdNtCommentPage tdp=tdNtReserveDao.findReserverById(tdNtCommentPage.getReserve_id());
            // 1 评论不能为空；2 评价学员是上课学员；3 评论状态为2（待评论）【Study_state：0找寻老师中1学习中2待支付3待评价4已完成5已完成】
            if (tdp != null && tdNtCommentPage.getMemb_id().equals(tdp.getMemb_id())) {
                //TdNtMembRecordingList tdNtMembRecordingList = tdNtCourseInstanceRecordingDao.findTdNtMembRecordingBySttId(tdNtCommentInsert.getStt_id());
                tdNtCommentPage.setCom_month(CommonUtil.getMonth(new Date()));
                //tdNtCommentInsert.setRecording_url(tdNtMembRecordingList.getRecording_url());
                tdNtCommentDao.insertComment(tdNtCommentPage);
                /**
                 * 修改课程状态
                 */
                tdNtReserveDao.updateStuIsComment(1,tdNtCommentPage.getReserve_id());
                /**
                 * 给老师端发送推送
                 */
                TdNtTeacherResult tdNtTeacherResult = tdNtTeacherDao.findTeacherByTeaId(tdNtCommentPage.getTea_id());
                if (tdNtTeacherResult.getEqu_client_id() != null && !(tdNtTeacherResult.getEqu_client_id().equals("1212313fasdf"))) {

                    TdNtMemberDetail tdNtMemberDetail = tdNtMemberDao.findTdNtMemberDetail(tdNtCommentPage.getMemb_id());
                    TsNtCourseInfoDetail tsNtCourseInfoDetail = tsNtCourseInfoDao.findCourseByCouId(tdNtCommentPage.getCou_id());

                    if (tdNtTeacherResult.getEqu_type().equals(Constants.equ_type_android)) {
                        AndroidCommentTeacher.apnpush("NativeTalk","One student sent you a teaching comment, check now! ",
                                tdNtTeacherResult.getEqu_client_id(),tdNtCommentPage.getReserve_id(),tdNtMemberDetail.getMemb_name(),tdNtMemberDetail.getMemb_head_portrait()
                                ,tdNtTeacherResult.getToken(),tdNtTeacherResult.getTea_head_portrait(),tdNtTeacherResult.getTea_name(),tsNtCourseInfoDetail.getCou_english_name());
                    } else {
                        logger.info(tdNtTeacherResult.getEqu_client_id());
                        IosCommentTeacher.apnpush("NativeTalk","One student sent you a teaching comment, check now! ",
                                tdNtTeacherResult.getEqu_client_id(),tdNtCommentPage.getReserve_id(),tdNtMemberDetail.getMemb_name(),tdNtMemberDetail.getMemb_head_portrait()
                                ,tdNtTeacherResult.getToken(),tdNtTeacherResult.getTea_head_portrait(),tdNtTeacherResult.getTea_name(),tsNtCourseInfoDetail.getCou_english_name());
                    }

                }

                retInfo.setMark(ValidationMessages.getValue("MARKYES"));
                retInfo.setTip("添加评论成功");
            } else {
                retInfo.setMark(ValidationMessages.getValue("MARKNO"));
                retInfo.setTip(ValidationMessages.getValue("TIPAUTHORERROR"));
            }

        } catch (Exception e) {
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
    /**
     * 返回体验课报告
     * @param memb_id
     * @return
     */
    public RetInfo returnCommentReport(Integer memb_id){
        RetInfo retInfo=new RetInfo();
        String logInfo = this.getClass().getName() + ":toTeaComment:";
        // 类路径+方法名
        logger.info("======" + logInfo + "begin======");
        try{
            TdNtTeacherComment tdNtTeacherComment=tdNtCommentDao.selectAllByLastCommentTime(memb_id);
            retInfo.setMark("0");
            retInfo.setTip("查询成功");
            retInfo.setObj(tdNtTeacherComment);
        }catch (Exception e){
            retInfo.setMark(ValidationMessages.getValue("MARKERROR"));
            retInfo.setTip(ValidationMessages.getValue("TIPERROR"));
            logger.error(e.getMessage(),e);
        }
        logger.info("======" + logInfo + "end======");
        return retInfo;
    }
}
